﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using FineUI;
using SDLITS.Model;
using SDLITS.Common;
using MySoft.Data;
using System.Collections;
using System.Data;
using System.Data.SqlClient;

namespace SDLITS.Web.LED
{
    public partial class LEDStatManage : CommonBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindCom();
                //BindGrid();
            }
        }
        protected void BindCom()
        {
            List<V_drpCompany> coms = SDDbSession.Default.From<V_drpCompany>().ToList();
            drp_com.DataSource = coms;
            drp_com.DataTextField = "Value";
            drp_com.DataValueField = "ID";
            drp_com.DataBind();
            drp_com.Items.Insert(0, new ListItem("--选择公司--", "-1"));
        }
        private void BindGrid()
        {
            DateTime startDate = Convert.ToDateTime("1900-01-01");
            DateTime endDate = Convert.ToDateTime("2900-01-01");
            if (!string.IsNullOrEmpty(txt_start.Text))
            {
                startDate = Convert.ToDateTime(txt_start.Text);
            }
            if (!string.IsNullOrEmpty(txt_end.Text))
            {
                endDate = Convert.ToDateTime(txt_end.Text).AddDays(1);
            }
            string companyID = "";
            if (drp_com.SelectedValue != "-1")
            {
                companyID = drp_com.SelectedValue;
            }
            //SqlParameter[] sp = new SqlParameter[]{
            //        new SqlParameter("@CompanyID",companyID),
            //        new SqlParameter("@StartDate",startDate),//@SPAction='SelectStat'
            //        new SqlParameter("@EndDate",endDate),
            //        new SqlParameter("@SPAction","SelectStat")
            //    };
            //DataSet ds = SDDbSession.Default.FromProc("TableLEDMessage").AddParameter(sp).ToDataSet();
            //ViewState["OutPut"] = ds.Tables[0];
            string sqlWhere = "";
            if (companyID != "")
                sqlWhere += " and CompanyID=" + companyID;
            DataTable ds = SDDbSession.Default.FromSql("select * from (select RoomName as Room,(select CompanyName from Company where a.RoomID=RoomID) as Company,(select CompanyID from Company where a.RoomID=RoomID) as CompanyID,(select count(0) from LEDMessage where a.RoomID=RoomID and CreateDate>'" + startDate + "' and CreateDate<'" + endDate + "') as MessageTotal from Room a) as t where Company is not null" + sqlWhere).ToDataTable();


            Grid1.DataSource = ds;
            Grid1.DataBind();
        }

        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid();
        }
        protected void btn_jf_Click(object sender, EventArgs e)
        {
            BindGrid();
            Grid1.Hidden = false;
        }

        protected void btn_tz_Click(object sender, EventArgs e)
        {
            var MyTable = new Hashtable { { "RoomID", "房间索引" }, { "Company", "公司" }, { "Room", "房间" }, { "MessageTotal", "发布条数" } };
            if (ViewState["OutPut"] != null)
            {
                Methed.GridViewToExcel((DataTable)ViewState["OutPut"], MyTable, "Excel报表");
            }
        }


    }
}
